package com.lebang.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.lebang.aop.Sessionid;
import com.lebang.common.Require;
import com.lebang.component.RedisClient;
import com.lebang.component.RedisSdkClient;
import com.lebang.entity.RequestHeaders;
import com.lebang.model.enums.SdkVideoEnums;
import com.lebang.service.AppVideoSortService;
import com.lebang.service.PointWallSdkService;
import com.lebang.service.SdkVideoService;
import com.lebang.service.TaskInRedisService;
import com.lebang.service.sys.DictService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static com.lebang.service.TaskInRedisService.HASH_CSJ;

/**
 * @Author: hwq
 * @Date: 2019/10/12 10:24
 * @Version 1.0
 */
@RestController
@RequestMapping("/sdk")
public class SdkUploadApi {
    public static final String ON_SDK_MEMBER = "ON_SDK_MEMBER";
    public static final String ON_SDK_MEMBER_ALL = "ON_SDK_MEMBER_ALL";
    public static final String EVENT_KEY = "EVENT_KEY:";
    public static final String CSJ = "CSJ:LOCK:COUNT:";
    /**
     * 看视频每日最大次数超出后没有奖励
     */
    public static final String MAX_VIDEO_COUNT = "max_video_count";
    @Autowired
    private TaskInRedisService taskInRedisService;
    @Autowired
    private RedisClient redisClient;
    @Autowired
    private RedisSdkClient sdkClient;
    @Autowired
    private SdkVideoService sdkVideoService;
    public static boolean STATUS = true;
    public static boolean VIDEOSTATUS = true;
    @Autowired
    private PointWallSdkService pointWallSdkService;
    @Autowired
    private DictService dictService;
    @Autowired
    private AppVideoSortService appVideoSortService;
    @Sessionid
    @PostMapping("/add/task")
    public JsonData task(HttpServletRequest request, String sdkKey) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        if("taojin".equals(sdkKey)){
            return null;
        }
        if (STATUS) {
            String userid = requestHeaders.getUserid();
            String imei = requestHeaders.getImei();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("memberId", userid);
            jsonObject.put("imei", imei);
            jsonObject.put("sdkKey", sdkKey);
            jsonObject.put("time", System.currentTimeMillis());
            jsonObject.put("type", 0);
            sdkClient.hset(ON_SDK_MEMBER, userid, jsonObject.toJSONString());
            jsonObject.put("type", 1);
            redisClient.setex(EVENT_KEY + jsonObject.toJSONString(), userid, 600);
        }
        return Callback.success("success", "查询成功", null);
    }

    @GetMapping("/add/enable")
    public JsonData enable(HttpServletRequest request, boolean status) {
        STATUS = status;
        if (STATUS = false) {
            sdkClient.del(ON_SDK_MEMBER, ON_SDK_MEMBER_ALL);
        }
        return Callback.success("success", "修改成功");
    }
    @Require
    @PostMapping("/video/getlist")
    public JsonData getlist(HttpServletRequest request) {


        return Callback.success("success", "查询成功", dictService.selectIntByKeyName(MAX_VIDEO_COUNT));
    }
    @Require
    @PostMapping("/csj/getcount")
    public JsonData getcount(HttpServletRequest request) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        String memberId = requestHeaders.getUserid();

        Integer count = 0;
        Object obj = redisClient.hget(HASH_CSJ, memberId);
        if (obj != null) {
            count = Integer.parseInt(obj.toString());
        }

        return Callback.success("success", "查询成功", count);
    }

    @Require
    @GetMapping("/csj/check")
    public JsonData check(HttpServletRequest request) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        String memberId = requestHeaders.getUserid();
        boolean on = redisClient.exists(CSJ + memberId);
        if (on) {
            return Callback.fail("201", "请休息一下,稍后再来哦", redisClient.ttl(CSJ + memberId));
        }
        if(sdkVideoService.checkMaxVideoCount(memberId)){
            return Callback.fail("200", "已经达到观看上限,明天再来");
        }
        JSONObject jsonObject = new JSONObject();
        String address = "";
        int count = 0;
        String obj = redisClient.hget(HASH_CSJ, memberId);
        if (obj != null) {
            count = Integer.parseInt(obj);
        }
        count = count + 1;
        String key=appVideoSortService.getByCount(count);
        if(key==null){
            address = "sdk://video";
        }else {
            address=key;
        }
        jsonObject.put("address", address);
        int money = pointWallSdkService.getMoneyByVideoCheck(memberId);
        jsonObject.put("money", money);
        return Callback.success("success", "验证通过", jsonObject);
    }

    @PostMapping("/insert")
    @Require
    @Sessionid
    public String insert(HttpServletRequest request, Integer index) {
        if(index==null){
            return "sb";
        }
        RequestHeaders requestHeaders = new RequestHeaders(request);
        String memberId = requestHeaders.getUserid();
        SdkVideoEnums enums = SdkVideoEnums.getByIndex(index);


        if(sdkVideoService.checkMaxVideoCount(memberId)){
            return "成功";
        }
        if (enums == null) {
            return "成功";
        }

        RequestHeaders header = new RequestHeaders(request);
        boolean on = redisClient.exists(CSJ + memberId);
        if (on) {
            return "成功";
        } else {

            int money = pointWallSdkService.getMoneyByVideoCheck(memberId);
            sdkVideoService.creat(memberId, money, enums, header);
            taskInRedisService.csjAdd(memberId,pointWallSdkService.getList());
            return "成功";
        }

    }

    @GetMapping("/add/videoenable")
    public JsonData videoenable(HttpServletRequest request, boolean status) {
        VIDEOSTATUS = status;
        return Callback.success("success", "修改成功");
    }
}
